package com.kailong.servlet.admin;

import com.kailong.dao.admin.ManageAdminDao;
import com.kailong.pojo.Admin;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/adminLoginServlet")
public class AdminLoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String name = req.getParameter("name");
        String pwd = req.getParameter("pwd");
        int count = ManageAdminDao.checkAdminByano(name,pwd);
        PrintWriter out = resp.getWriter();
        if (count>0){
            HttpSession session = req.getSession();
            Admin admin = ManageAdminDao.selectAdmin(name,pwd);
            session.setAttribute("ano",admin.getAno());
            session.setAttribute("aname",admin.getAname());
            session.setAttribute("aphonenumber",admin.getAphonenumber());
            resp.sendRedirect("http://localhost:8080/Library/admin/adminManage.jsp");
            session.setAttribute("isAdminLogin",1);

        }else{
            out.print("<script>");
            out.print("alert('用户登录失败！');");
            out.print("location.href='http://localhost:8080/Library/adminLogin.jsp';");
            out.print("</script>");
        }

    }
}
